<template>
  <f7-page
    :name="name"
    @page:init="onPageInit"
    @page:beforein="onPageBeforeIn"
    @page:afterin="onPageAfterIn"
    @page:beforeout="onPageBeforeOut"
    @page:afterout="onPageAfterOut"
  >
    <div class="home-div">课程管理系统</div>
  </f7-page>
</template>

<script>
import Log from "../mod/log.js";
import Consts from "../assets/consts";

import strings from "../assets/strings";
import cordova from "../js/cordova-app";

import Alert from "../mod/alert";
import Toast from "../mod/toast";
import Storage from "../mod/storage";
import AlertUtils from "../mod/utils.alert";
import ToastUtils from "../mod/utils.toast";
import Utils from "../js/mod.utils";
import StringUtils from "../js/mod.string";
import ConstantUtils from "../js/vue.constant";
import Http from "../js/mod.http";

import WeChatUtils from "../js/wechat";

const name = strings.page.home;
const log = new Log(name);

export default {
  data() {
    return {
      name: name,
      lastBackTimestamp: new Date().getTime(),
      code: "",
      id: "",
      from: "wechat",
      model: "",
      push_type: "",
      openid: "",
      userinfo: {},
    };
  },

  methods: {
    onCreated() {
      var _this = this;
      log.w("onCreated");
    },

    onMounted() {
      log.w("onMounted");
      var _this = this;
    },

    onPageInit() {
      log.w("onPageInit");
    },

    onPageBeforeIn() {
      log.w("onPageBeforeIn");
    },

    onPageAfterIn() {
      log.w("onPageAfterIn");
      var _this = this;
      Utils.setTitle(_this, "课程管理系统");
      Utils.setBack(_this, function (e) {
        Utils.close_window(_this); // 关闭页面
      });
      var dataStr = Utils.GetQueryString("data");
      var init = Storage.sessionGet("init");
      if (dataStr) {
        //var b = new Base64();
        //dataStr = b.decode(dataStr); //解密
        //Storage.sessionSet("dataStr", dataStr);
        //window.location.href = "./";
        //return false;
      }
      //dataStr = Storage.sessionGet("dataStr");

      //dataStr = '{"model":"edu","openid":"oz5Gfs7wpqbapFIK6gMX7LwsIqTs"}';
      //dataStr = '{"model":"edu","openid":"oz5GfswzKaz3csCvOiHFP6Btr2-k"}';
      //alert('dataStr=' + dataStr + '-' + _this.$f7.data.from);
      if (dataStr) {
        //Storage.sessionSet("init", "init");
        var mobile = localStorage.getItem("mobile");
        if (mobile) {
          // 存在手机号
          _this.openid = mobile;
          _this.$f7.data.openid = mobile;

          _this.tipPage();
        } else {
          // 去登录
          _this.tipBind();
        }

        _this.themeColor(); // 主题色
        // 判断是否绑定
        //_this.getWechatIsBind();
      } else {
        if (Consts.debug) {
          Utils.load(_this, Consts.debugPage, {}, {});
        } else {
          Utils.close_window(_this); // 关闭页面
        }
      }
    },

    // 跳转页面
    tipPage: function () {
      var _this = this;
      Utils.load(_this, "/edu-list/", {}, {});
    },

    // 判断是否绑定
    getWechatIsBind: function () {
      var _this = this;
      var data = {
        openid: _this.openid,
      };
      let params = {
        url: Http.kInterfacePath.wechat.is_bind,
        data: data,
        type: Http.httpType.post,
        success: _successFun,
      };
      Http.ajax(_this, params);

      function _successFun(res) {
        if (res.result == 0) {
          var data = res.data || {};
          var is_bind = data.is_bind || false;

          if (is_bind) {
            //绑定了
            _this.tipPage();
          } else {
            // 未绑定
            _this.tipBind();
          }
        } else {
          ToastUtils.toast.show(
            ToastUtils.param.toast({ text: "查询信息失败" })
          );
        }
      }
    },

    // 跳转到绑定页面
    tipBind: function () {
      var _this = this;
      Utils.load(_this, "/wechat-bind/", {}, {});
    },

    themeColor: function () {
      var _this = this;
      // 按钮主题色
      let barColor = "blue";
      if (_this.from == "wechat") {
        // 微信
        barColor = "green";
      }
      let currentColorClass = _this.$f7.root[0].className.match(
        /color-theme-([a-z]*)/
      );
      if (currentColorClass) {
        _this.$f7.root.removeClass(currentColorClass[0]);
      }
      _this.$f7.root.addClass(`color-theme-${barColor}`);
    },

    onAlertConfirm(dialog, e) {
      log.d("confirm", dialog, e);
    },

    onAlertCancel(dialog, e) {
      log.d("cancel", dialog, e);
    },

    onPageBeforeOut() {
      log.w("onPageBeforeOut");
    },

    onPageAfterOut() {
      log.w("onPageAfterOut");
    },

    onDestroyed() {
      log.w("onDestroyed");
    },

    onUpdated() {
      log.w("onUpdated");
    },

    onBack() {
      log.w("onBack");
    },

    onResume() {
      log.w("onResume");
    },

    onPause() {
      log.w("onPause");
    },
  },

  created() {
    this.onCreated();
  },

  updated() {
    this.onUpdated();
  },

  destroyed() {
    this.onDestroyed();
  },

  mounted() {
    this.onMounted();
  },
};
</script>